<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------


namespace app\common\model;


use app\common\lib\Status;
use think\Exception;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * 有关时间的模型
 * Class TimeModel
 * @package app\common\model
 */
class TimeModel extends Model
{

    /**
     * 自动时间戳类型
     * @var string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 添加时间
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 软删除
     */
    use SoftDelete;

    /**
     * @var bool
     */
    protected $deleteTime = false;

    /**
     * 公共查询 正常数据
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getBaseByNormalList(array $where = [], string $field = "*", string $orderBy = "id desc"): array
    {
        try {
            $lists = $this
                ->field($field)
                ->where($where)
                ->where('status', Status::mysql_normal)
                ->order($orderBy)
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return TimeModel|array|mixed|Model|null
     * @throws Exception
     */
    public function getBaseByNormalListFind(array $where = [], string $field = "*", string $orderBy = "id desc")
    {
        try {
            $lists = $this
                ->field($field)
                ->where($where)
                ->where('status', Status::mysql_normal)
                ->order($orderBy)
                ->find();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * @param array $where
     * @param int $limit
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getPaginateByNormalListInfo(array $where = [], int $limit = 10, string $field = "*", string $orderBy = "id desc"): array
    {
        try {
            $lists = $this
                ->field($field)
                ->where($where)
                ->where('status', Status::mysql_normal)
                ->order($orderBy)
                ->paginate($limit)
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * @param int $id
     * @param string $field
     * @return TimeModel|array|mixed|Model|null
     * @throws Exception
     */
    public function getBaseById(int $id = 0,string $field='*'){
        try {
            $lists = $this
                ->field($field)
                ->where('id',$id)
                ->find();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

}